<?php $gridId=rand(0,10000); ?>
<script type="text/javascript">
Ext.onReady(function(){
Ext.QuickTips.init();
Ext.BLANK_IMAGE_URL = '<?=$this->baseUrl?>/public/images/default/s.gif';



var test=new Ext.FormPanel({
		labelAlign:'left',
		frame:true,
		plain:true,
		id:'user_add',
		buttonAlign:'center',  
		bodyStyle:'padding:0 10px 0',
		width:830,
		autoHeight:true,
		resizable:true,
		items:[{html:"<iframe width=830 height=500 frameborder=0 scrolling=auto src='<?=$this->baseUrl?>/admin/controlpage'></iframe>"
			   }]
	});

var form_add=new Ext.FormPanel({								   
		labelAlign:'left',
		frame:true,
		plain:true,
		id:'user_add',
		buttonAlign:'center',  
		bodyStyle:'padding:0 10px 0',
		width:630,
		autoHeight:true,
		resizable:true,
		items:[{
			   layout:'fit',
			   width:600,
			   height:70,
			   border:false,
			   items:[{
					  xtype:'fieldset',
					  title:'权限组',
					  height:60,
					  items:[
							 {	xtype:'textfield',
							 	name:'name',
								width:200,
							 	fieldLabel:'权限组名'
							 }
							 ]					  
					  }]
			   },
			   {
				   xtype:'fieldset',
				   title:'权限设置',
				   autoHeight:true,
				   layout:'form',
				   items:[
						  {
							  xtype:'checkboxgroup',
							  fieldLabel:"产品信息<br />(全选)<input type='checkbox' name='product'   />",
							  items:[
									 {boxLabel:'查看',name:'product_view'},
									 {boxLabel:'修改',name:'product_edit'},
									 {boxLabel:'删除',name:'product_del'},
									 {boxLabel:'添加',name:'product_add'}
									 ]
							  },
							  {
							  xtype:'checkboxgroup',
							  fieldLabel:"订单信息<br />(全选)<input  type='checkbox'  name='order'   />",							
							  columns: 4,
							  vertical: true,
							  items:[
									 {boxLabel:'8001',value:'8001',name:'view[]'},
									 {boxLabel:'8002',value:'8002',name:'view[]'},
									 {boxLabel:'8003',value:'8003',name:'view[]'},
									 {boxLabel:'8004',value:'8004',name:'view[]'},
									 {boxLabel:'8005',value:'8005',name:'view[]'},
									 {boxLabel:'8007',value:'8007',name:'view[]'},
									 {boxLabel:'7001',value:'7001',name:'view[]'},
									 {boxLabel:'7002',value:'7002',name:'view[]'},
									 {boxLabel:'7003',value:'7003',name:'view[]'},
									 {boxLabel:'7004',value:'7004',name:'view[]'},
									 {boxLabel:'7005',value:'7005',name:'view[]'}
									 ]
								  },
							{
							  xtype:'checkboxgroup',
							  fieldLabel:"管理<br />(全选)<input type='checkbox' name='product'   />",
							  items:[
									 {boxLabel:'用户信息',name:'user'},
									 {boxLabel:'权限信息',name:'control'}
									 ]
								}
						  ]
				   }
				],
		buttons:[{
				 text:'save',
				 name:'submit',
				 id:'save',
				 handler:function(){
					  form_add.getForm().submit({
							waitTitle: '提示',
							waitMsg:'等待中',
							url:'<?=$this->baseUrl?>/user/add',
							method:'post',
							success:function(form,action){
								var temp=Ext.util.JSON.decode(action.response.responseText);
								//Ext.MessageBox.alert('成功',temp.msg);
								win_user_add.hide();
								//ds.load({params:{start:0,limit:30}});
								//grid.render();
								},
							failure:function(form,action){
								var temp=Ext.util.JSON.decode(action.response.responseText);
								Ext.MessageBox.alert('waring',temp.msg);
								}
							});
					 }
				 },
				 {
					text:'cancel',	 
					name:'cancel',
					handler:function(){
						win_user_add.hide();
						}
				}
				 ]
	});
var win_user_add=new Ext.Window({	
												id			:'win_show',
								 				resizable	: true,
												width		: 830,	
												title		: '用户权限管理',
												autoHeight	: true,
												items		: [test],
												buttonAlign	: 'center',
												listeners	: {"close":function(){
													//alert("关闭");
													//win_user_add.hide();													
													}}
													});
var ds=new Ext.data.Store({
							  proxy:new Ext.data.HttpProxy({
									url:'<?=$this->baseUrl?>/admin/roledata/'
									}),
							  reader:new Ext.data.JsonReader({
									root:'results',
									totalProperty:'total',
									id:'user_id'
									},[
									   {name:'role_id',mapping:'role_id'},
									   {name:'role_name',mapping:'role_name'}
									   ])
	});		 
//定义列模型
var cm=new Ext.grid.ColumnModel([									 
									  {header:'ID',dataIndex:'role_id',width:200},
									 {header:'权限组',dataIndex:'role_name',width:150}
	]);
cm.defaultSortable = true; 


//每页显示记录数
	var mypagedata=[
				['10','10'],
				 ['15','15'],
				 ['20','20'],
				 ['40','40'],
				 ['200','200']
				 ];	
var page_size_store=new Ext.data.SimpleStore({
									   fields:[
											   {name:'id'},
											   {name:'name'}
									   		  ]
									   });
page_size_store.loadData(mypagedata);
var pagesize_combo=new Ext.form.ComboBox({
										 store:page_size_store,
										 width:50,										
										 emptyText:'10',
										 mode:'local',
										 triggerAction:'all',
										 valueField:'name',
										 displayField:'id'
										 });
pagesize_combo.on('select',function(e){										
										var pagesize=parseInt(e.getValue());
										pagBar.pageSize=pagesize;
										ds.load({params:{start:0,limit:pagesize}});
										//grid.render();
										});	

//定义分页条栏				 
var pagBar=new Ext.PagingToolbar({
										pageSize:30,
										store:ds,
										displayInfo:true,
										displayMsg:'第{0}到{1}条记录 共{2}条记录',
										emptyMsg:'没有数据记录',
										//plugins: filters,
										items:['-','每页显示',pagesize_combo,'条记录','-']
									 });
//定义工具栏								 
var tb=new Ext.Toolbar({
							items:[
								   		 {
											text:'增加',
											iconCls:'icn-add',
											handler:function(){
													win_user_add.show();		
												}
										},
										  {
											text:'删除',
											iconCls:'icn-delete',
											handler:function(){												
													var selectRows=grid.getSelectionModel().getSelections();												
												if(selectRows.length==0){
													Ext.MessageBox.alert('提示','请选择一条数据');
													}
												else{		
												//Ext.MessageBox.confirm("确认么","确定要提交么",showResult);	
													var message="";
													for(var i=0;i<selectRows.length;i++){
														if(i!=0){
															message+=",";
															}
														message+=selectRows[i].get('role_id');
														}
													Ext.Ajax.request({
																	 url:'<?=$this->baseUrl?>/user/delrole',
																	 params:{id:message},																																		 																	success:function(response,option){
																var temp=Ext.util.JSON.decode(response.responseText);																
																ds.load({params:{start:0,limit:30}});
																grid.render();
																		 },
																	failure:function(response,option){
																		var temp=Ext.util.JSON.decode(response.responseText);
																		Ext.MessageBox.alert('waring',temp.msg);
																		}
																	 });
													//Ext.MessageBox.alert('执行','你选择的是第'+message+'条数据');
													}
												}
											  },{text:'修改',
											  	iconCls:'icn-edit',
												handler:menuEdit
											  }
								 ]
	});									
//定义容器
var grid=new Ext.grid.GridPanel({
									renderTo:'grid_<?=$gridId?>',
									store:ds,
									cm:cm,									
									height:500,
									minHeight:300,
									//autoHeight:true,
									autoWidht:true,								
									//plugins: filters,
									loadMask:{msg:'加载服务端数据中',store:ds},	
									autoScroll:true,
									stripeRows:true,									
        							animCollapse: false,
									//sm:new Ext.grid.CheckboxSelectionModel({singleSelect:false}),
									monitorWindowResize: true,	
									title:'用户列表',
									sm:new Ext.grid.CheckboxSelectionModel({singleSelect:false}),
									tbar:tb,
									bbar:pagBar,
									stateful:true,  
									stateId:'cookiegrid' 
									});
//显示数据
ds.load({params:{start:0,limit:30}});
grid.render();


function menuEdit(){
	var selectRows=grid.getSelectionModel().getSelections();												
		if(selectRows.length==0 || selectRows.length>1){
			Ext.MessageBox.alert('提示','请选择一条数据');
		}else{	
		var role_id=selectRows[0].get('role_id');
		var role_name=selectRows[0].get('role_name');
		var win=new Ext.Window({
						width:800,
						height:400,
						id:'win_show',
						frame:true,
						plain:true,						
						buttonAlign:'center',  
						bodyStyle:'padding:0 10px 0',
						autoHeight:true,
						resizable:true,
						items:[{html:"<iframe width=800 height=400 frameborder=0 scrolling=auto src='<?=$this->baseUrl?>/index/controlpage/id/"+role_id+"/name/"+role_name+"'></iframe>"}]
						});
		win.show();
	}
}
//绑定事件
//Ext.get('test').on("click",function(){
//									 alert("fff");
//									 if(this.dom.checked){
//										 Ext.select('input[name^=view]').each(function(){this.dom.checked=true;});										 
//									}else{
//										 Ext.select('input[name^=view]').each(function(){this.dom.checked=false;});						
//										}
//									 
//									 });



	
});

</script>
<body style="width:98%;height:100%">
<div id='grid_<?=$gridId?>'></div>
</body>
</html>